<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id= trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
#如果有group_id
if ($id > 0) {
	$sql = "SELECT * FROM customers WHERE id=$id";
	$_company = $database->queryFirst($sql);
	if (!$_company['id']) {
		$_REQUEST['referrer'] = "index.php";
		redirect("记录不存在!");
	}
}
#得到post的值
$company_no = trim($_REQUEST['company_no']);
	$quote['company_no'] = string_sql($company_no);
$password = trim($_REQUEST['password']);
$company_name = trim($_REQUEST['company_name']);
	$quote['company_name'] = string_sql($company_name);
$company_legal = trim($_REQUEST['company_legal']);
	$quote['company_legal'] = string_sql($company_legal);
$company_region= trim($_REQUEST['company_region']);
	$quote['company_region'] = string_sql($company_region);
$company_capital = intval(trim($_REQUEST['company_capital']));
	$quote['company_capital'] = string_sql($company_capital);
$contact_username = trim($_REQUEST['contact_username']);
	$quote['contact_username'] = string_sql($contact_username);
$contact_tel = trim($_REQUEST['contact_tel']);
	$quote['contact_tel'] = string_sql($contact_tel);
$company_address = trim($_REQUEST['company_address']);
	$quote['company_address'] = string_sql($company_address);
$company_phone = trim($_REQUEST['company_phone']);
	$quote['company_phone'] = string_sql($company_phone);
$company_fax = trim($_REQUEST['company_fax']);
	$quote['company_fax'] = string_sql($company_fax);
$company_zipcode= trim($_REQUEST['company_zipcode']);
	$quote['company_zipcode'] = string_sql($company_zipcode);
$company_email = trim($_REQUEST['company_email']);
	$quote['company_email'] = string_sql($company_email);
$company_url = trim($_REQUEST['company_url']);
	$quote['company_url'] = string_sql($company_url);
$industry = trim($_REQUEST['industry']);
	$quote['industry'] = string_sql($industry);
$industry_detail = trim($_REQUEST['industry_detail']);
	$quote['industry_detail'] = string_sql($industry_detail);
$company_staff = intval(trim($_REQUEST['company_staff']));
	$quote['company_staff'] = string_sql($company_staff);
$turnover = intval(trim($_REQUEST['turnover']));
	$quote['turnover'] = string_sql($turnover);
$businesstime = intval(trim($_REQUEST['businesstime']));
	$quote['businesstime'] = string_sql($businesstime);
$demand = trim($_REQUEST['demand']);
	$quote['demand'] = string_sql($demand);
$services = trim($_REQUEST['services']);
	$quote['services'] = $services;
 $region = trim($_REQUEST['region']);
	$quote['region'] = string_sql($region);
#如果是编辑
if ($action == "edit.form" || $action == "view.form" || $action == "members.form") {
	$company_no = $_company['company_no'];
        $company_name = $_company['company_name'];
        $company_legal = $_company['company_legal'];
        $company_region = $_company['company_region'];
        $company_capital = $_company['company_capital'];
        $contact_username = $_company['contact_username'];
        $contact_tel = $_company['contact_tel'];
        $company_address = $_company['company_address'];
        $company_phone = $_company['company_phone'];
        $company_fax = $_company['company_fax'];
        $company_zipcode= $_company['company_zipcode'];
        $company_email =$_company['company_email'];
        $company_url = $_company['company_url'];
        $industry = $_company['industry'];
        $industry_detail = $_company['industry_detail'];
        $company_staff = $_company['company_staff'];
        $turnover = $_company['turnover'];
        $businesstime = $_company['businesstime'];
        $demand =$_company['demand'];
        $services = $_company['services'];
        $region = $_company['region'];
}
#如果是添加操作
if ($action == "add.do") {
	verify_field("company_no", "企业注册号", "empty");
	verify_field("password", "密码", "empty");
	verify_field("company_name", "企业名称", "empty");
        verify_field("contact_username", "联系人", "empty");
        verify_field("company_capital", "注册资本", "number_only");
        verify_field("contact_tel", "联系电话", "empty");
        verify_field("company_email", "企业电子邮箱", "email");
        verify_field("company_staff", "企业员工人数", "number_only");
        verify_field("turnover", "年营业额", "number_only");
        verify_field("businesstime", "营业时间", "number_only");

	if (!count($error_strs)) {
		$sql = "INSERT INTO customers ".
                "(company_no, password, company_name,company_legal,company_region,company_capital,contact_username,contact_tel".
                ",company_address,company_phone,company_fax,company_zipcode,company_email,company_url".
                ",industry,industry_detail,company_staff,turnover,businesstime,demand,services,region".
                " ) VALUES (".$quote['company_no'].", ".string_sql(md5($password)).", ".$quote['company_name'].", ".
                        $quote['company_legal'].", ".$quote['company_region'].", ".$quote['company_capital'].", ".$quote['contact_username'].", ".
                        $quote['contact_tel'].", ".$quote['company_address'].", ".$quote['company_phone'].", ".
                        $quote['company_fax'].", ".$quote['company_zipcode'].", ".$quote['company_email'].", ".
                        $quote['company_url'].", ".$quote['industry'].", ".$quote['industry_detail'].", ".
                        $quote['company_staff'].", ".$quote['turnover'].", ".$quote['businesstime'].", ".
                        $quote['demand'].", '".$quote['services']."', ".$quote['region'].");";
		$database->execute($sql);		
		$_REQUEST['referrer'] = "index.php";
		redirect("添加企业客户成功!");
	} else {
		$action = "add.form";
	}
#如果是编辑操作
} else if ($action == "edit.do" && $id > 0) {

	verify_field("company_no", "企业注册号", "empty");
	verify_field("company_name", "企业名称", "empty");
        verify_field("contact_username", "联系人", "empty");
        verify_field("company_capital", "注册资本", "number_only");
        verify_field("contact_tel", "联系电话", "empty");
        verify_field("company_email", "企业电子邮箱", "email");
        verify_field("company_staff", "企业员工人数", "number_only");
        verify_field("turnover", "年营业额", "number_only");
        verify_field("businesstime", "营业时间", "number_only");

	if (!count($error_strs)) {
		$sql = "UPDATE customers SET company_no = ".$quote['company_no'].iif(strlen($password)>0, ",password=".string_sql(md5($password))).
                ", company_name = ".$quote['company_name'].", company_legal = ".$quote['company_legal'].", company_region = ".$quote['company_region'].", company_capital = ".$quote['company_capital'].", contact_username = ".$quote['contact_username'].
                ", contact_tel = ".$quote['contact_tel'].", company_address = ".$quote['company_address'].", company_phone = ".$quote['company_phone'].", company_fax = ".$quote['company_fax'].
                ", company_zipcode = ".$quote['company_zipcode'].", company_email = ".$quote['company_email'].", company_url = ".$quote['company_url'].", industry = ".$quote['industry'].
                ", industry_detail = ".$quote['industry_detail'].", company_staff = ".$quote['company_staff'].", turnover = ".$quote['turnover'].", businesstime = ".$quote['businesstime'].
                ", demand = ".$quote['demand'].", services = '".$quote['services']."', region = ".$quote['region'].
                " WHERE id = ".$quote['id'].";";
                $rs = $database->execute($sql);
		$_REQUEST['referrer'] = "index.php";
		redirect("修改企业客户成功!");
	} else {
		$action = "edit.form";
	}
#如果是删除操作
} else if ($action == "delete.do" && $id > 0) {
	
	$database->execute("DELETE FROM customers WHERE id = ".$quote['id'].";");
        $_REQUEST['referrer'] = "index.php";
        redirect("删除企业客户成功!");
}else if($action == 'members.form'){
        #翻页
    $page = trim($_GET['page']) + 0; if ($page == "") { $page = $_COOKIE['cmpage']; }
    $pagesize = 20;
    #!!翻页
    if ($page < 1) $page = 1;

    #总共数量
    $rs_count = $database->queryArray("SELECT count(*) total FROM members_info WHERE company=".string_sql($company_name));
    $total_page = ceil($rs_count[0]["total"] / $pagesize);
    if ($total_page < 1) $total_page = 1;
    if ($page > $total_page) $page = $total_page;
    set_cookie('cmpage', $page, time() + 604800);
}
#样式
$css = array();
$js = array("tiny_mce/tiny_mce");
#菜单
$menu = "company";
include_once("../layout/_header.php");
?>
<div class="main">
<div class="traininc-nav-list">
<ul>
<?php
if($action == "add.form"){
?>
<li><a href="index.php">企业客户列表</a></li>
<li class="selected"><a href="javascript:;">新增企业客户</a></li>
<?php
}elseif($action == "edit.form"){
?>
<li><a href="index.php">企业客户列表</a></li>
<li class="selected"><a href="javascript:;">修改企业客户</a></li>
<?php
}elseif($action == "members.form"){
?>
<li><a href="edit.php?action=view.form&id=<?=$id?>">企业详细信息</a></li>
<li class="selected"><a href="javascript:;">员工列表</a></li>
<?php
}else{
?>
<li class="selected"><a href="javascript:;">企业详细信息</a></li>
<li><a href="edit.php?action=members.form&id=<?=$id?>">员工列表</a></li>
<?php
}
?>
</ul>
</div>
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    language : "zh-cn",
    theme : "advanced",
    mode: "exact",
    elements : "services",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"300px",
    width:"600px"
});
</script>
<?
if ($action == "add.form") {
?>
<form action="edit.php?action=add.do" method="post" id="form" name="form">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">企业注册号</td>
                <td><input type="text" name="company_no" size="50" value="<?= htmlspecialchars($company_no) ?>"> <?= show_field_error('company_no') ?></td>
        </tr>
        <tr>
                <td class="title">企业密码</td>
                <td><input type="password" name="password" size="50" value=""><?= show_field_error('password') ?></td>
        </tr>
        <tr>
                <td class="title">企业名称</td>
                <td><input type="text" name="company_name" size="50" value="<?= htmlspecialchars($company_name) ?>"> <?= show_field_error('company_name') ?></td>
        </tr>
        <tr>
                <td class="title">法人</td>
                <td><input type="text" name="company_legal" size="50" value="<?= htmlspecialchars($company_legal) ?>"></td>
        </tr>
        <tr>
                <td class="title">注册地</td>
                <td><input type="text" name="company_region" size="50" value="<?= htmlspecialchars($company_region) ?>"></td>
        </tr>
        <tr>
                <td class="title">注册资本</td>
                <td><input type="text" name="company_capital" size="50" value="<?= htmlspecialchars($company_capital) ?>"> <?= show_field_error('company_capital') ?></td>
        </tr>
        <tr>
                <td class="title">联系人</td>
                <td><input type="text" name="contact_username" size="50" value="<?= htmlspecialchars($contact_username) ?>"> <?= show_field_error('contact_username') ?></td>
        </tr>
        <tr>
                <td class="title">联系人电话</td>
                <td><input type="text" name="contact_tel" size="50" value="<?= htmlspecialchars($contact_tel) ?>"> <?= show_field_error('contact_tel') ?></td>
        </tr>

        <tr>
                <td class="title">企业地址</td>
                <td><input type="text" name="company_address" size="50" value="<?= htmlspecialchars($company_address) ?>"></td>
        </tr>
        <tr>
                <td class="title">邮编</td>
                <td><input type="text" name="company_zipcode" size="50" value="<?= htmlspecialchars($company_zipcode) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业电话</td>
                <td><input type="text" name="company_phone" size="50" value="<?= htmlspecialchars($company_phone) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业传真</td>
                <td><input type="text" name="company_fax" size="50" value="<?= htmlspecialchars($company_fax) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业电子邮件</td>
                <td><input type="text" name="company_email" size="50" value="<?= htmlspecialchars($company_email) ?>"> <?= show_field_error('company_email') ?></td>
        </tr>
        <tr>
                <td class="title">企业官方网站</td>
                <td><input type="text" name="company_url" size="50" value="<?= htmlspecialchars($company_url) ?>"></td>
        </tr>
        <tr>
                <td class="title">行业</td>
                <td><input type="text" name="industry" size="50" value="<?= htmlspecialchars($industry) ?>"></td>
        </tr>
        <tr>
                <td class="title">具体</td>
                <td><input type="text" name="industry_detail" size="50" value="<?= htmlspecialchars($industry_detail) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业员工人数</td>
                <td><input type="text" name="company_staff" size="50" value="<?= htmlspecialchars($company_staff) ?>"> <?= show_field_error('company_staff') ?></td>
        </tr>
        <tr>
        <td class="title">年营业额</td>
        <td><input type="text" name="turnover" size="50" value="<?= htmlspecialchars($turnover) ?>"> <?= show_field_error('turnover') ?></td>
        </tr>
        <tr>
        <td class="title">经营时间</td>
        <td><input type="text" name="businesstime" size="50" value="<?= htmlspecialchars($businesstime) ?>"><?= show_field_error('businesstime') ?></td>
        </tr>
        <tr>
                <td class="title">服务需求</td>
                <td><input type="text" name="demand" size="50" value="<?= htmlspecialchars($demand) ?>"></td>
        </tr>
        <tr>
        <td class="title">网点</td>
        <td>
            <select name="region">
            <?php
            #获得网点
            $rgs = $database->queryArray("SELECT * FROM region");
            for($i=0;$i<count($rgs);$i++){
                $rg = $rgs[$i];
                echo "<option value='".$rg['name']."'>".$rg['name']."</option>";
            }
            ?>
            </select>
        </td>
</tr>
        <tr>
                <td class="title">服务内容</td>
                <td><textarea name="services" cols="60" rows="7"><?= $services ?></textarea></td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
        </tr>
</table>
</div>
</form>
<?
} else if ($action == "edit.form") {
?>
<form action="edit.php?action=edit.do" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-input">
        <tr>
                <td class="title">企业注册号</td>
                <td><input type="text" name="company_no" size="50" value="<?= htmlspecialchars($company_no) ?>"> <?= show_field_error('company_no') ?></td>
        </tr>
        <tr>
                <td class="title">企业密码</td>
                <td><input type="password" name="password" size="50" value=""><?= show_field_error('password') ?></td>
        </tr>
        <tr>
                <td class="title">企业名称</td>
                <td><input type="text" name="company_name" size="50" value="<?= htmlspecialchars($company_name) ?>"> <?= show_field_error('company_name') ?></td>
        </tr>
        <tr>
                <td class="title">法人</td>
                <td><input type="text" name="company_legal" size="50" value="<?= htmlspecialchars($company_legal) ?>"></td>
        </tr>
        <tr>
                <td class="title">注册地</td>
                <td><input type="text" name="company_region" size="50" value="<?= htmlspecialchars($company_region) ?>"></td>
        </tr>
        <tr>
                <td class="title">注册资本</td>
                <td><input type="text" name="company_capital" size="50" value="<?= htmlspecialchars($company_capital) ?>"> <?= show_field_error('company_capital') ?></td>
        </tr>
        <tr>
                <td class="title">联系人</td>
                <td><input type="text" name="contact_username" size="50" value="<?= htmlspecialchars($contact_username) ?>"> <?= show_field_error('contact_username') ?></td>
        </tr>
        <tr>
                <td class="title">联系人电话</td>
                <td><input type="text" name="contact_tel" size="50" value="<?= htmlspecialchars($contact_tel) ?>"> <?= show_field_error('contact_tel') ?></td>
        </tr>

        <tr>
                <td class="title">企业地址</td>
                <td><input type="text" name="company_address" size="50" value="<?= htmlspecialchars($company_address) ?>"></td>
        </tr>
        <tr>
                <td class="title">邮编</td>
                <td><input type="text" name="company_zipcode" size="50" value="<?= htmlspecialchars($company_zipcode) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业电话</td>
                <td><input type="text" name="company_phone" size="50" value="<?= htmlspecialchars($company_phone) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业传真</td>
                <td><input type="text" name="company_fax" size="50" value="<?= htmlspecialchars($company_fax) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业电子邮件</td>
                <td><input type="text" name="company_email" size="50" value="<?= htmlspecialchars($company_email) ?>"> <?= show_field_error('company_email') ?></td>
        </tr>
        <tr>
                <td class="title">企业官方网站</td>
                <td><input type="text" name="company_url" size="50" value="<?= htmlspecialchars($company_url) ?>"> <?= show_field_error('company_url') ?></td>
        </tr>
        <tr>
                <td class="title">行业</td>
                <td><input type="text" name="industry" size="50" value="<?= htmlspecialchars($industry) ?>"></td>
        </tr>
        <tr>
                <td class="title">具体</td>
                <td><input type="text" name="industry_detail" size="50" value="<?= htmlspecialchars($industry_detail) ?>"></td>
        </tr>
        <tr>
                <td class="title">企业员工人数</td>
                <td><input type="text" name="company_staff" size="50" value="<?= htmlspecialchars($company_staff) ?>"> <?= show_field_error('company_staff') ?></td>
        </tr>
        <tr>
        <td class="title">年营业额</td>
        <td><input type="text" name="turnover" size="50" value="<?= htmlspecialchars($turnover) ?>"> <?= show_field_error('turnover') ?></td>
        </tr>
        <tr>
        <td class="title">经营时间</td>
        <td><input type="text" name="businesstime" size="50" value="<?= htmlspecialchars($businesstime) ?>"> <?= show_field_error('businesstime') ?></td>
        </tr>
        <tr>
                <td class="title">服务需求</td>
                <td><input type="text" name="demand" size="50" value="<?= htmlspecialchars($demand) ?>"></td>
        </tr>
            <tr>

                <td class="title">网点</td>
        <td>
            <select name="region">
            <?php
            #获得网点
            $rgs = $database->queryArray("SELECT * FROM region");
            for($i=0;$i<count($rgs);$i++){
                $rg = $rgs[$i];
                echo "<option value='".$rg['name']."' ".iif($rg['name']==$region, "selected='selected'")." >".$rg['name']."</option>";
            }
            ?>
            </select>
        </td>
            </tr>
        <tr>
                <td class="title">服务内容</td>
                <td><textarea name="services" cols="60" rows="7"><?= $services ?></textarea></td>
        </tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="修改" class="btnA" /></td>
        </tr>
</table>
</div>
</form>
<?php
}elseif ($action == "view.form") {
?>
<form action="edit.php?action=edit.form" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-view">
    <tr>
            <td class="title">企业注册号</td>
            <td><?= htmlspecialchars($company_no) ?> </td>
            <td class="title">网点</td>
            <td><?= htmlspecialchars($region) ?></td>
    </tr>
    <tr>
            <td class="title">企业名称</td>
            <td><?= htmlspecialchars($company_name) ?></td>
            <td class="title">法人</td>
            <td><?= htmlspecialchars($company_legal) ?></td>
    </tr>
    <tr>
            <td class="title">注册地</td>
            <td><?= htmlspecialchars($company_region) ?></td>
            <td class="title">注册资本</td>
            <td><?= htmlspecialchars(number_format($company_capital)) ?> 元</td>
    </tr>
    <tr>
            <td class="title">联系人</td>
            <td><?= htmlspecialchars($contact_username) ?></td>
            <td class="title">联系人电话</td>
            <td><?= htmlspecialchars($contact_tel) ?></td>
    </tr>
    <tr>
            <td class="title">企业地址</td>
            <td><?= htmlspecialchars($company_address) ?></td>
            <td class="title">邮编</td>
            <td><?= htmlspecialchars($company_zipcode) ?></td>
    </tr>
    <tr>
            <td class="title">企业电话</td>
            <td><?= htmlspecialchars($company_phone) ?></td>
            <td class="title">企业传真</td>
            <td><?= htmlspecialchars($company_fax) ?></td>
    </tr>
    <tr>
            <td class="title">企业电子邮箱</td>
            <td><?= htmlspecialchars($company_email) ?></td>
            <td class="title">企业官方网站</td>
            <td><?= htmlspecialchars($company_url) ?></td>
    </tr>
    <tr>
            <td class="title">行业</td>
            <td><?= htmlspecialchars($industry) ?></td>
            <td class="title">具体</td>
            <td><?= htmlspecialchars($industry_detail) ?></td>
    </tr>
    <tr>
            <td class="title">企业员工人数</td>
            <td><?= htmlspecialchars($company_staff) ?> 人</td>
            <td class="title">年营业额</td>
            <td><?= htmlspecialchars(number_format($turnover)) ?> 元</td>
    </tr>
    <tr>
            <td class="title">经营时间</td>
            <td><?= htmlspecialchars($businesstime) ?> 年</td>
            <td class="title"></td>
            <td></td>
    </tr>
    <tr>
            <td class="title">服务需求</td>
            <td><?= htmlspecialchars($demand) ?></td>
            <td class="title"></td>
            <td></td>
    </tr>
    <tr>
            <td class="title">服务内容</td>
            <td><?= $services ?></td>
            <td class="title"></td>
            <td></td>
    </tr>
    <tr>
            <td class="title"> </td>
            <td><input type="submit" name="" value="修改" class="btnA" /></td>
            <td class="title"></td>
            <td></td>
    </tr>
</table>
</div>
</form>
<?php
}else{
$rsc = $database->queryArray("SELECT * FROM members_info WHERE company=".string_sql($company_name)." LIMIT ".(($page - 1) * $pagesize).", $pagesize");
?>
				<table class="table-blue">
					<thead>
						<tr>
							<th>编号</th>
							<th>姓名</th>
							<th>手机</th>
							<th>网点</th>
							<th>卡号</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<?
                                                for($i=0;$i<sizeof($rsc);$i++){
                                                $r = $rsc[$i];
                                                $j = 1;
                                                ?>
                                                <tr <?=iif($j%2==0, 'class="odd"')?>>
                                                        <td><?=$r['id']?></td>
                                                        <td><?=$r['name']?></td>
                                                        <td><?=$r['tel']?></td>
                                                        <td><?=$r['region']?></td>
                                                        <td><?=$r['card']?></td>
                                                        <td><a href="<?=_CONST_BACKEND_URL?>members/edit.php?action=view.form&id=<?= $r['id'] ?>">查看详细</a></td>
                                                </tr>
                                                <?
                                                $j++;
                                                }
                                                ?>
					</tbody>
				</table>
                               <div class="digg">
                                 <? pagination($page, $pagesize, $rs_count[0]["total"], "edit.php?action=members.form&page=%s%") ?>
                               </div>
<?php
}
?>
</div>
<?
include_once("../layout/_footer.php");
?>